#!/usr/bin/perl
#Match Regular Expression - m//
#Substitute Regular Expression - s///
#Transliterate Regular Expression - tr///




@list = qw/food foosball subeo footnote terfoot canic footbrdige/;

foreach (@list)
{
   $first = $1 if /(foo.*)/;
   $last = $1 if /(foo.*)/;
}
print "First: $first, Last: $last\n";


#The Substitution Operator
#s/PATTERN/REPLACEMENT/;

$string = 'The cat sat on the mat';
$string =~ s/cat/dog/;

print "Final Result is $string\n";



#Translation
#tr/SEARCHLIST/REPLACEMENTLIST/cds
#y/SEARCHLIST/REPLACEMENTLIST/cds


$string = 'The cat sat on the mat';
$string =~ tr/a/o/;

print "$string\n";


$string = 'the cat sat on the mat.';
$string =~ tr/a-z/b/d;

print "$string\n";


$string = 'food';
$string = 'food';
$string =~ tr/a-z/a-z/s;

print $string;


#complex regular expressions

$string = "Cats go Catatonic\nWhen given Catnip";
($start) = ($string =~ /\A(.*?) /);
@lines = $string =~ /^(.*?) /gm;
print "First word: $start\n","Line starts: @lines\n";

#Matching Boundaries

/\bcat\b/; # Matches 'the cat sat' but not 'cat on the mat'
/\Bcat\B/; # Matches 'verification' but not 'the cat on the mat'
/\bcat\B/; # Matches 'catatonic' but not 'polecat'
/\Bcat\b/; # Matches 'polecat' but not 'catatonic'

#Selecting Alternatives
if (($string =~ /Martin Brown/) ||($string =~ /Sharon Brown/))
{
print "hello"
}
#Grouping Matching

$string =~ /(\S+)\s+(\S+)/;

$date = '03/26/1999';
$date =~ s#(\d+)/(\d+)/(\d+)#$3/$1/$2#;

print "$date";

#Using the \G Assertion

$string = "The time is: 12:31:02 on 4/12/00";

$string =~ /:\s+/g;
($time) = ($string =~ /\G(\d+:\d+:\d+)/);
$string =~ /.+\s+/g;
($date) = ($string =~ m{\G(\d+/\d+/\d+)});

print "Time: $time, Date: $date\n";



#Regular Expression Variables
$string = "The food is in the salad bar";
$string =~ m/foo/;
print "Before: $`\n";
print "Matched: $&\n";
print "After: $'\n";
